package com.masterclient.app.modules.aftersale;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.eoner.baselibrary.bean.aftersale.ExpressBean;
import com.eoner.common.config.Config;
import com.eoner.managerlibrary.sobot.SobotManager;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.eoner.baselib.utils.notify.RxBus;
import com.masterclient.app.modules.aftersale.contract.LogistContract;
import com.masterclient.app.modules.aftersale.contract.LogistPresenter;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.sobot.chat.SobotApi;
import com.sobot.chat.api.enumtype.SobotChatTitleDisplayMode;
import com.sobot.chat.api.model.Information;
import java.util.HashMap;
import java.util.Map;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：sean
 * 创建时间：2017/8/1 15:38
 * 功能说明：
 */
public class LogistInfoActivity extends BaseActivity<LogistPresenter> implements LogistContract.View {


    @BindView(R.id.rl_back)
    RelativeLayout rlBack;
    @BindView(R.id.rl_message)
    RelativeLayout rlMessage;
    @BindView(R.id.tv_state)
    TextView tvState;
    @BindView(R.id.tv_company)
    TextView tvCompany;
    @BindView(R.id.tv_no)
    TextView tvNo;
    @BindView(R.id.ll_list)
    LinearLayout llList;
    @BindView(R.id.rl_empty)
    RelativeLayout rlEmpty;
    @BindView(R.id.iv_unread)
    ImageView ivUnread;
    @BindView(R.id.tv_copy)
    TextView tvCopy;
    @BindView(R.id.iv_kefu)
    ImageView ivKefu;

    private boolean showKefu;
    private RxBus rxBus;
    String tracker_num = "", tracker_company = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logist_info);
        ButterKnife.bind(this);
        showKefu = getIntent().getBooleanExtra("showKefu",true);
        mPresenter.getTrackDetail(getBundleString("order_id"),getBundleString("track_no"),getBundleString("biz_type"));

        rxBus = RxBus.$();
        rxBus.OnEvent(rxBus.register(Config.MSG_UNREAD), o -> {
            if (Config.UNREAD > 0) {
                ivUnread.setVisibility(View.VISIBLE);
            } else {
                ivUnread.setVisibility(View.INVISIBLE);
            }
        });
        if(!showKefu){// 隐藏客服图标
            ivKefu.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onDestroy() {
        rxBus.unregister(Config.MSG_UNREAD);
        super.onDestroy();
    }

    @OnClick({R.id.rl_back, R.id.rl_message, R.id.tv_copy})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_copy:
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                // 将文本内容放到系统剪贴板里。
                cm.setPrimaryClip(ClipData.newPlainText(null, tracker_num));
                showToast("已复制到粘贴板");
                break;
            case R.id.rl_back:
                finish();
                break;
            case R.id.rl_message:
                Information info = SobotManager.getBaseInfo();
                //SobotApi.setChatTitleDisplayMode(mContext, SobotChatTitleDisplayMode.ShowFixedText, "物流信息");
                SobotApi.setChatTitleDisplayMode(mContext, SobotChatTitleDisplayMode.Default, "");
                if (!TextUtils.isEmpty(tracker_num)) {
                    Map<String, String> customInfo = new HashMap<String, String>();
                    customInfo.put("tracking_number", tracker_num);
                    customInfo.put("logistics_company", tracker_company);
                    info.setCustomInfo(customInfo);
                }
                SobotApi.startSobotChat(LogistInfoActivity.this, info);
//                CustomerService.getInstance(mContext).connectService("物流信息", null, Config.CUSTOMER);
                break;
        }
    }

    @Override
    public void getTrackDetail(ExpressBean bean) {
        tracker_num = bean.trackNo;
        tracker_company = bean.shipmentCompany;
        String company = "物流公司：";
        if (TextUtils.isEmpty(bean.shipmentCompany)) {
            tvCompany.setText(company);
        } else {
            tvCompany.setText(String.format("%s%s", company, bean.shipmentCompany));
        }
        tvState.setText(bean.trackStatusDesc);
        String logistId = "物流单号：";
        if (TextUtils.isEmpty(bean.trackNo)) {
            tvNo.setText(logistId);
        } else {
            tvNo.setText(String.format("%s%s", logistId, bean.trackNo));
        }

        if (bean.trackList != null && bean.trackList.size() >= 1) {
            for (int i = 0; i < bean.trackList.size(); i++) {
                ExpressBean.TrackBean trackBean = bean.trackList.get(i);
                View view = View.inflate(mContext, R.layout.item_wuliu, null);
                View v_top = view.findViewById(R.id.v_top);
                View v_bottom = view.findViewById(R.id.v_bottom);
                View v_short_bottom = view.findViewById(R.id.v_short_bottom);
                View v_long_bottom = view.findViewById(R.id.v_long_bottom);
                ImageView iv_tag = view.findViewById(R.id.iv_tag);
                TextView tv_desc = view.findViewById(R.id.tv_desc);
                TextView tv_start_time = view.findViewById(R.id.tv_start_time);
                if (i == 0) {
                    v_top.setVisibility(View.INVISIBLE);
                    iv_tag.setImageResource(R.mipmap.img1_shouhou);
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) iv_tag.getLayoutParams();
                    params.width = ScreenUtils.dp2px(26);
                    params.height = ScreenUtils.dp2px(26);
                    iv_tag.setLayoutParams(params);
                } else if (i == (bean.trackList.size() - 1)) {
                    v_bottom.setVisibility(View.INVISIBLE);
                    iv_tag.setImageResource(R.mipmap.img3_shouhou);
                    v_short_bottom.setVisibility(View.VISIBLE);
                    tv_desc.setTextColor(0xff999999);
                    tv_start_time.setTextColor(0xff999999);
                    v_long_bottom.setVisibility(View.INVISIBLE);
                } else {
                    tv_desc.setTextColor(0xff999999);
                    tv_start_time.setTextColor(0xff999999);
                }
                tv_desc.setText(trackBean.context);
                tv_start_time.setText(trackBean.time);
                llList.addView(view);
            }
        } else {
            llList.setVisibility(View.GONE);
            rlEmpty.setVisibility(View.VISIBLE);
        }
    }
}
